#include <math.h>
#include <iostream>

using std::cout;
using std::endl;

class Point;//类的前向声明

class Line
{
public:
    //此时没有看到Point的定义，所以Point此时还是一个不完整类型
    double distance(const Point &lhs, const Point &rhs);
    double distance(const Point &rhs);
    /* { */
        /* Point pt;//不完整类型是不能创建对象的 */
        /* Point *ppt;//不完整类型是可以创建指针的 */
        /* return hypot(lhs._ix - rhs._ix, lhs._iy - rhs._iy); */
    /* } */
};

class Point
{
    //友元之成员函数
    friend double Line::distance(const Point &lhs, const Point &rhs);
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int = 0, int = 0)" << endl;
    }

    void print() const
    {
        cout << "(" << this->_ix 
             << ", " << this->_iy
             << ")";
    }

    ~Point()
    {
        cout << "~Point()" << endl;
    }

private:
    int _ix;
    int _iy;
};

//2、友元的第二种形式：友元之成员函数
double Line::distance(const Point &lhs, const Point &rhs)
{
    return hypot(lhs._ix - rhs._ix, lhs._iy - rhs._iy);
}

double Line::distance(const Point &rhs)
{
    /* return hypot(_ix - rhs._ix, _iy - rhs._iy);//error */
}

Point func()
{
    Point pt(1, 2);
    return pt;
}

void test()
{
    /* Line line;//左值，对象 */

    Point pt1(1, 2);
    Point pt2(4, 6);
    pt1.print();
    cout << "---->";
    pt2.print();
    /* cout << "之间的距离 : " << line.distance(pt1, pt2) << endl; */
    cout << "之间的距离 : " << Line().distance(pt1, pt2) << endl;
}


int main(int argc, char **argv)
{
    test();
    return 0;
}

